package com.giscloud.amp.service.auth.impl;

import com.giscloud.amp.dao.GisTableColumnMapper;
import com.giscloud.amp.dao.YunServiceBuilddictCfgMapper;
import com.giscloud.amp.entity.YunServiceBuilddictCfg;
import com.giscloud.amp.entity.YunServiceBuilddictCfgExample;
import com.giscloud.amp.service.auth.GisTableColumnService;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
public class GisTableColumnServiceImpl implements GisTableColumnService {

    private static final String YUN_BUILDDICT = "yun:builddict:";
//
//    @Autowired
//    private RedisUtils redisUtils;

    @Autowired
    private YunServiceBuilddictCfgMapper yunServiceBuilddictCfgMapper;


    @Autowired
    private GisTableColumnMapper gisTableColumnMapper;
    /**
     * 获取gis属性项列表
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> getColumnList(Map<String, Object> params) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("list",gisTableColumnMapper.getColumnList(params));
        resultMap.put("count",gisTableColumnMapper.getColumnListCount(params));
        return resultMap;
    }

    /**
     * 获取table项
     * @return
     */
    @Override
    public Map<String, Object> getTableList() {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("list",gisTableColumnMapper.getTableList());
        return resultMap;
    }

    /**
     * 删除配置
     * @param cfgId
     * @return
     */
    @Override
    public int deleteByPrimaryKey(Long cfgId) {
        YunServiceBuilddictCfg yunServiceBuilddictCfg = yunServiceBuilddictCfgMapper.selectByPrimaryKey(cfgId);
        if(yunServiceBuilddictCfg != null){
            yunServiceBuilddictCfgMapper.deleteByPrimaryKey(cfgId);
        }
        return 1;
    }

    @Override
    public int insert(YunServiceBuilddictCfg record) {
        yunServiceBuilddictCfgMapper.insert(record);
        return 1;
    }

    @Override
    public PageInfo selectByExample(Map<String, Object> params) {
        YunServiceBuilddictCfgExample yunServiceBuilddictCfgExample = new YunServiceBuilddictCfgExample();
        YunServiceBuilddictCfgExample.Criteria criteria = yunServiceBuilddictCfgExample.createCriteria();
        if(params.get("tableName") != null && !"".equals(String.valueOf(params.get("tableName")))){
            criteria.andTableNameLike("%"+String.valueOf(params.get("tableName")).toUpperCase()+"%");
        }
        PageHelper.startPage((int)params.get("page"),(int)params.get("rows"));
        List<YunServiceBuilddictCfg> list = yunServiceBuilddictCfgMapper.selectByExample(yunServiceBuilddictCfgExample);
        return PageInfo.of(list);
    }

    @Override
    public YunServiceBuilddictCfg selectByPrimaryKey(Long cfgId) {
        return yunServiceBuilddictCfgMapper.selectByPrimaryKey(cfgId);
    }

    @Override
    public int updateByExampleSelective(YunServiceBuilddictCfg record) {
        YunServiceBuilddictCfgExample yunServiceBuilddictCfgExample = new YunServiceBuilddictCfgExample();
        if(record.getCfgId() != null){
            yunServiceBuilddictCfgExample.createCriteria().andCfgIdEqualTo(record.getCfgId());
            yunServiceBuilddictCfgMapper.updateByExampleSelective(record,yunServiceBuilddictCfgExample);
        }
        return 1;
    }
}
